using System;
using System.Collections.Generic;
using System.Text;
using XEngine;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace XEnginePhysicsGame
{
    public class InputProcessor
    {
        XMain X;
        Game1 Parent;

#if XBOX == FALSE
        XMouse Mouse;
        XKeyboard Keyboard;
#endif
        XGamePad Gamepad;

        public InputProcessor(XMain X, Game1 Parent)
        {
            this.X = X;
            this.Parent = Parent;
        }

        public void Load()
        {
#if XBOX == FALSE
            Mouse = new XMouse(X);
            Keyboard = new XKeyboard(X);
#endif
            Gamepad = new XGamePad(X, 1);
        }

        bool jumping;

        public void Update(GameTime gameTime)
        {
            Parent.Camera.TargetPosition = Parent.scene.Ball.Position;

            Vector3 ballPos = new Vector3(Parent.scene.Ball.Position.X, 0, Parent.scene.Ball.Position.Z);
            Vector3 camPos = new Vector3(Parent.Camera.Position.X, 0, Parent.Camera.Position.Z);
            Vector3 dir = (ballPos - camPos);

#if XBOX == FALSE
            Parent.Camera.Rotate(Mouse.Delta.X * .008f, Mouse.Delta.Y * .008f);
            Parent.Camera.Zoom(-Mouse.ScrollDelta * .015f);

            if (!jumping && Keyboard.KeyPressed(Keys.Space))
            {
                jumping = true;
                Parent.scene.Ball.PhysicsBody.PhysicsBody.ApplyBodyImpulse(Vector3.Transform(new Vector3(0, 600000, 0), Matrix.Invert(Parent.scene.Ball.Orientation)));
            }
            else if (jumping)
            {
                if (Parent.scene.Ball.Collisions.Count > 0)
                    jumping = false;
            }

            Parent.scene.Ball.PhysicsBody.PhysicsBody.ApplyBodyImpulse(Vector3.Transform(dir * (Keyboard.KeyDown(Keys.W) ? 1 : 0) * 1000, Matrix.Invert(Parent.scene.Ball.Orientation)));
            Parent.scene.Ball.PhysicsBody.PhysicsBody.ApplyBodyImpulse(-Vector3.Transform(dir * (Keyboard.KeyDown(Keys.S) ? 1 : 0) * 1000, Matrix.Invert(Parent.scene.Ball.Orientation)));
            Parent.scene.Ball.PhysicsBody.PhysicsBody.ApplyBodyImpulse(Vector3.Transform(dir * (Keyboard.KeyDown(Keys.A) ? 1 : 0) * 1000, Matrix.Invert(Parent.scene.Ball.Orientation * Matrix.CreateRotationY(MathHelper.ToRadians(-90)))));
            Parent.scene.Ball.PhysicsBody.PhysicsBody.ApplyBodyImpulse(Vector3.Transform(dir * (Keyboard.KeyDown(Keys.D) ? 1 : 0) * 1000, Matrix.Invert(Parent.scene.Ball.Orientation * Matrix.CreateRotationY(MathHelper.ToRadians(90)))));

            X.Debug.Write("Words", false);
#endif
            Parent.Camera.Rotate(-Gamepad.Thumbstick(XGamePad.Thumbsticks.Right).X * .04f, Gamepad.Thumbstick(XGamePad.Thumbsticks.Right).Y * .04f);
            Parent.Camera.Zoom((Gamepad.ButtonDown(Buttons.DPadDown) ? 0.15f : 0) + (Gamepad.ButtonDown(Buttons.DPadUp) ? -0.15f : 0));

            if (!jumping && Gamepad.ButtonPressed(Buttons.A))
            {
                jumping = true;
                Parent.scene.Ball.PhysicsBody.PhysicsBody.ApplyBodyImpulse(Vector3.Transform(new Vector3(0, 600000, 0), Matrix.Invert(Parent.scene.Ball.Orientation)));
            }
            else if (jumping)
            {
                if (Parent.scene.Ball.Collisions.Count > 0)
                    jumping = false;
            }

            Parent.scene.Ball.PhysicsBody.PhysicsBody.ApplyBodyImpulse(Vector3.Transform(dir * Gamepad.Thumbstick(XGamePad.Thumbsticks.Left).Y * 1000, Matrix.Invert(Parent.scene.Ball.Orientation)));
            Parent.scene.Ball.PhysicsBody.PhysicsBody.ApplyBodyImpulse(-Vector3.Transform(dir * -Gamepad.Thumbstick(XGamePad.Thumbsticks.Left).Y * 1000, Matrix.Invert(Parent.scene.Ball.Orientation)));
            Parent.scene.Ball.PhysicsBody.PhysicsBody.ApplyBodyImpulse(Vector3.Transform(dir * Gamepad.Thumbstick(XGamePad.Thumbsticks.Left).X * 1000, Matrix.Invert(Parent.scene.Ball.Orientation * Matrix.CreateRotationY(MathHelper.ToRadians(90)))));
            Parent.scene.Ball.PhysicsBody.PhysicsBody.ApplyBodyImpulse(-Vector3.Transform(dir * -Gamepad.Thumbstick(XGamePad.Thumbsticks.Left).X * 1000, Matrix.Invert(Parent.scene.Ball.Orientation * Matrix.CreateRotationY(MathHelper.ToRadians(90)))));
        }
    }
}
